package admin

import (
	"geekai/api/dto/request"
	"geekai/api/handler"
	"geekai/core"
	"geekai/core/types"
	"geekai/service"
	"geekai/utils"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"strings"
)

// PromptHandler 提示词生成
// 使用AI生成生成提示词指令
type PromptHandler struct {
	handler.BaseHandler
}

func NewPromptHandler(app *core.AppServer, db *gorm.DB) *PromptHandler {
	return &PromptHandler{
		BaseHandler: handler.BaseHandler{App: app, DB: db},
	}
}

// MetaPrompt godoc
// @Summary      生成元提示词
// @Description  生成元提示词
// @Tags         后台管理-提示词生成
// @Accept       json
// @Produce      json
// @Param        req   body     request.PromptGenerateReq  true  "提示词信息"
// @Success      200  {object}  types.BizVo{data=string}
// @Router       /api/admin/prompt/meta [post]
// @Security AdminApiKeyAuth
func (h *PromptHandler) MetaPrompt(c *gin.Context) {
	var data request.PromptGenerateReq
	if err := c.ShouldBindJSON(&data); err != nil {
		utils.ERROR(c, types.InvalidArgs)
		return
	}

	messages := make([]interface{}, 0)
	messages = append(messages, types.Message{
		Role:    "system",
		Content: service.MetaPromptTemplate,
	})
	messages = append(messages, types.Message{
		Role:    "user",
		Content: "Task, Goal, or the Role to actor is:\n" + data.Prompt,
	})
	content, err := utils.SendOpenAIMessage(h.DB, messages, 0)
	if err != nil {
		utils.ERROR(c, "生成元提示词失败："+err.Error())
		return
	}
	utils.SUCCESS(c, strings.Trim(content, `"`))
}
